<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>冒泡算法</title>
    <script>
        var array = [12, 512, 5, 12, 5, 325, 43, 6, 54, 7, 56, 8, 76, 97, 8];
        var sum = 0;
        console.log(array);
        for (var i = 0; i < array.length; i++) {
            for (var j = i; j < array.length; j++) {
                if (array[i] < array[j]) {
                    var flag = array[i];
                    array[i] = array[j];
                    array[j] = flag;
                }
                sum++;
            }
        }
        console.log(array);
        console.log('自己的算法:' + sum);
        sum = 0;
        var array1 = [12, 512, 5, 12, 5, 325, 43, 6, 54, 7, 56, 8, 76, 97, 8];
        console.log(array1);
        for (var i = 1; i < array1.length; i++) {
            for (var j = 0; j < array1.length - i; j++) {
                if (array1[j] < array1[j + 1]) {
                    var flag = array1[j];
                    array1[j] = array1[j + 1];
                    array1[j + 1] = flag;
                }
                sum++;
            }
        }
        console.log(array1);
        console.log('冒泡排序循环次数:' + sum);
    </script>

</head>

<body>
</body>

</html>